package it.orma.iridehr.validators;

import it.orma.iridehr.entities.DatiGenerali;

import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.util.regex.Pattern;

import javax.activation.MimetypesFileTypeMap;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;

import org.apache.commons.lang3.StringUtils;
import org.springframework.binding.message.MessageBuilder;
import org.springframework.binding.message.MessageContext;
import org.springframework.binding.validation.ValidationContext;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

@Component
public class DatiGeneraliValidator {
	
	private static final String EMAIL_PATTERN = 
			"^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@"
			+ "[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
	
	private Pattern pattern;
	
	public DatiGeneraliValidator() {
		pattern = Pattern.compile(EMAIL_PATTERN);
	}
	
	public void validate(DatiGenerali datiGenerali, ValidationContext context) {
		
		
		MessageContext messages = context.getMessageContext();
		if (!datiGenerali.isFotoUpload()){		
			if (StringUtils.isEmpty(datiGenerali.getCognome())) {
				messages.addMessage(new MessageBuilder().error().source("cognome")
						.code("error.campo.obbligatorio").build());
			}
			if (StringUtils.isEmpty(datiGenerali.getNome())) {
				messages.addMessage(new MessageBuilder().error().source("nome")
						.code("error.campo.obbligatorio").build());
			}
	
			if (StringUtils.isEmpty(datiGenerali.getComuneDiNascita())) {
				messages.addMessage(new MessageBuilder().error()
						.source("comuneDiNascita").code("error.campo.obbligatorio")
						.build());
			}
			if (StringUtils.isEmpty(datiGenerali.getProvinciaDiNascita())) {
				messages.addMessage(new MessageBuilder().error()
						.source("provinciaDiNascita")
						.code("error.campo.obbligatorio").build());
			}
			if (datiGenerali.getDataDiNascita() == null) {
				messages.addMessage(new MessageBuilder().error()
						.source("dataDiNascita").code("error.campo.obbligatorio")
						.build());
			}
			if (StringUtils.isEmpty(datiGenerali.getMail1())){
				messages.addMessage(new MessageBuilder().error().source("mail1")
						.code("error.campo.obbligatorio").build());
			} else if (!pattern.matcher(datiGenerali.getMail1()).matches() ) {
				messages.addMessage(new MessageBuilder().error().source("mail1")
						.code("error.campo.nonvalido").build());
			}
			
			if ( datiGenerali.getIdDatiGenerali()!=null){
				if (StringUtils.isEmpty(datiGenerali.getSesso())) {
					messages.addMessage(new MessageBuilder().error().source("sesso")
							.code("error.campo.obbligatorio").build());
				}
				
				if (datiGenerali.isHaFigli()){
					if (datiGenerali.getNumeroFigli()==null || datiGenerali.getNumeroFigli()==0){
						messages.addMessage(new MessageBuilder().error().source("numeroFigli")
							.code("error.campo.obbligatorio").build());
					}
				}
				
				if (StringUtils.isNotEmpty(datiGenerali.getMail2()) && !pattern.matcher(datiGenerali.getMail2()).matches()){
					messages.addMessage(new MessageBuilder().error().source("mail2")
							.code("error.campo.nonvalido").build());
				}
			}
		}else{
			try{
				BufferedImage bufferedImage = ImageIO.read(new ByteArrayInputStream(((MultipartFile)context.getUserValue("multipartFileUpload")).getBytes()));
				if(bufferedImage.getWidth()>200||bufferedImage.getWidth()<10||
						bufferedImage.getHeight()>200||bufferedImage.getHeight()<10){
					messages.addMessage(new MessageBuilder().error().source("uploadFoto").defaultText("Immagine non valida")
							.code("error.campo.nonvalido").build());
				}					
					
			}catch(Exception e){
				messages.addMessage(new MessageBuilder().error().source("uploadFoto").defaultText("Immagine non valida")
						.code("error.campo.nonvalido").build());
			}
		
		}
	}
}
